<template>
  <div class="login-container">
    <!-- 背景视频 -->
    <div class="background-video">
      <video src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4" loop autoplay muted />
    </div>
    <!-- 背景阴影 -->
    <div class="shadow" />

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <div class="title">
          <img src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png" alt="">
        </div>
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.account"
          placeholder="账号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <!-- 验证码 -->
      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="code" />
        </span>
        <el-input v-model="loginForm.code" placeholder="验证码" />

        <img class="code-image" src="https://slwl-geteway.itheima.net/manager/captcha?key=23213872677&0=23213872677" alt="">
        <!-- <img class="code-image" :src="codeUrl" alt=""> -->
      </el-form-item>

      <el-button class="login-btn" :loading="loading" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { getCodeApi, loginApi } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: 'shenlingadmin',
        password: '123456',
        code: ''
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', message: '不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '不能为空' }],
        code: [{ required: true, trigger: 'blur', message: '不能为空' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      codeUrl: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // this.getCode()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async handleLogin() {
      await this.$refs.loginForm.validate()
      const res = await loginApi({ ...this.loginForm, key: '23213872677' })
      // console.log(res)
      await this.$store.commit('user/SETTOKEN', res)
      this.$router.push('/')
    },
    async getCode() {
      // const windowUrl = window.URL || window.webkitURL
      const res = await getCodeApi({ key: 2321387267777, 0: 2321387267777 })
      this.codeUrl = window.URL.createObjectURL(new Blob([res]))
      console.log(this.codeUrl)
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  display: flex;
  align-items: center;
  .background-video,.shadow{
    position: fixed;
    width: 100%;
    height: 100%;
  }
  .shadow{
    opacity: .3;
    background-color: #1a1c41;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #606266;
      height: 47px;
      // caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #eee;
    border-radius: 5px;
    color: #454545;
  }
  .code-image{
    position: absolute;
    width: 71px;
    height: 32px;
    right: 10px;
    top: 10px;
  }
  .login-btn{
    height: 54px;
    color: #fff;
    background-color: #e15536;
    font-size: 18px;
    font-weight: 700;
  }
}

</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    padding: 36px 40px;
    width: 450px;
    height: 467px;
    border-radius: 10px;
    margin: 0 auto;
    overflow: hidden;
    background-color: #fff;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      height: 58px;
      margin: 0px auto 40px auto;
      text-align: center;
      img{
        height: 100%;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
